﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Company.Merchandising.BusinessLayer.DataEntities;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.DataAccessLayer.DataAccessCommon;
using Company.Merchandising.ServiceLayer.EntityManagement.Constants;

namespace Company.Merchandising.ServiceLayer.EntityManagement
{
	public class ContactPersonManager : BaseManager
	{
		public ContactPersonManager(MerchModelEntities modelEntities) : base(modelEntities) { }

		public void AddConcactPerson(DTOContactPerson dtoContactPerson)
		{
			var contactPerson = new ContactPerson();
			contactPerson.Name = dtoContactPerson.Name;
			contactPerson.Surname = dtoContactPerson.Surname;
			contactPerson.Description = dtoContactPerson.Description;
			contactPerson.Email = dtoContactPerson.Email;
			contactPerson.Phone = dtoContactPerson.Phone;
			contactPerson.MobilePhone = dtoContactPerson.MobilePhone;
			contactPerson.Fax = dtoContactPerson.Fax;

			_modelEntities.AddObject(contactPerson.GetType().Name, contactPerson);
			_modelEntities.SaveChanges();
		}

		public void DeleteContactPerson(int contactPersonId)
		{
			DACContactPerson dacContactPerson = new DACContactPerson(_modelEntities);
			var contactPerson = dacContactPerson.GetContactPersonById(contactPersonId);
			if (contactPerson == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ContactPersonDoesNotExistAnyMore, contactPersonId));
			}

			if (!contactPerson.Locations.IsLoaded)
			{
				contactPerson.Locations.Load();
			}

			if (contactPerson.Locations.Count > 0)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ContactPersonPossessesLocations, contactPersonId));
			}

			if (!contactPerson.Departments.IsLoaded)
			{
				contactPerson.Departments.Load();
			}

			if (contactPerson.Departments.Count > 0)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ContactPersonPossessesDepartments, contactPersonId));
			}

			_modelEntities.DeleteObject(contactPerson);
			_modelEntities.SaveChanges();
		}

		public void UpdateContactPerson(DTOContactPerson dtoContactPerson)
		{
			DACContactPerson dacContactPerson = new DACContactPerson(_modelEntities);
			var contactPerson = dacContactPerson.GetContactPersonById(dtoContactPerson.Id);
			if (contactPerson == null)
			{
				throw new InvalidOperationException(string.Format(ExceptionMessages.ContactPersonDoesNotExistAnyMore, dtoContactPerson.Id));
			}

			contactPerson.Name = dtoContactPerson.Name;
			contactPerson.Surname = dtoContactPerson.Surname;
			contactPerson.Description = dtoContactPerson.Description;
			contactPerson.Email = dtoContactPerson.Email;
			contactPerson.Phone = dtoContactPerson.Phone;
			contactPerson.MobilePhone = dtoContactPerson.MobilePhone;
			contactPerson.Fax = dtoContactPerson.Fax;

			_modelEntities.SaveChanges();
		}
	}
}
